package com.example.richobject;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Socket
 */
public class SocketClient {

    private int CODE_MSG = 0x1001;
    Socket socket = new Socket();
    private final int HANDLER_MSG_TELL_RECV = 0x124;
    private OutputStream outputStream = null;
    private InputStream inputStream;
    byte[] bytes = new byte[1024];
    private Message msg;

    /**
     * 连接服务器并且监听
     *
     * @param handler
     * @param host
     * @param port
     */
    public void startNetThread(final Handler handler, final String host, final int port) {
        new Thread() {
            @Override
            public void run() {
                try {
                    LogUtil.e("链接socket开始收集数据");
                    if (!socket.isConnected()) {
                        socket.connect(new InetSocketAddress(host, port), 3000);
                    }
                    inputStream = socket.getInputStream();
                    while (true) {
                        msg = new Message();
                        // 获取读取流
                        if (inputStream.read() == 88) {
                            LogUtil.e("socket链接成功msg-->"+ msg);
                            msg.what = CODE_MSG;
                            msg.obj = String.valueOf(99);
                            handler.sendMessage(msg);
                        }
                    }
                } catch (Exception e) {
                    LogUtil.e("socket链接失败-->" + e.getMessage());
                    try {
                        socket.connect(new InetSocketAddress(host, port), 3000);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        LogUtil.e("socket链接又失败-->" + e.getMessage());
                    }
                }
            }
        }.start();
    }

    /**
     * 异步发送信息
     *
     * @param data
     */
    public void sendData(final byte[] data, final String host, final int port) {
        new Thread() {
            @Override
            public void run() {
                try {
                    if (!socket.isConnected()) {
                        socket.connect(new InetSocketAddress(host, port), 3000);
                    }
                    outputStream = socket.getOutputStream();
                    outputStream.write(data);
                    outputStream.flush();
                } catch (Exception e) {
                    LogUtil.e("socket异步发送信息失败-->" + e.getMessage());
                }
            }
        }.start();
    }

    /**
     * 断开连接
     */
    public void closeSocket() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void startReader(final Socket socket) {
        new Thread() {
            @Override
            public void run() {
                DataInputStream reader;
                try {
                    // 获取读取流
                    reader = new DataInputStream(socket.getInputStream());
                    while (true) {
                        System.out.println("*等待客户端输入*");
                        // 读取数据
                        String msg = reader.readUTF();
                        System.out.println("获取到客户端的信息：" + msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
